<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% 
String idLokalu=request.getParameter("local_id");
int local_id = Integer.parseInt(idLokalu);
%>
    <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						UserMethods um= new UserMethods();
						
						LocalMethods lm = new LocalMethods();
							response.setContentType("text/html");
							Local l = lm.findLocalById(sessionFactory, s, local_id);
							FeedbackMethods fb = new FeedbackMethods();
							List<LocalFeedback> listfb = fb.listLocalFeedbacks(sessionFactory, s, local_id);
							int commentsCount = fb.countFbByLocalId(sessionFactory, s, local_id);
							Integer user = (Integer) session.getAttribute("user_id");
					%> 
					<%
		String localName = l.getCity() + ", "+ l.getStreet()+ " "+l.getLocal_number();
		%>
<div class="page-header">
	<h2 class="page-title">Nasze lokale  -  <%=localName %></h2>
</div>
<div id="content">
<br />

			<table>

<tr>
<td>
		<div class="col-md-4 col-sm-6" style="width: 666px;">
											<div class="team-member">
												 <div class="member-thumb">
												<!--	<img src="images/team3.jpg" alt=""> -->
												</div>
												<div class="member-infos">
													<h4 class="member-name">
													<%out.println("<a class=\"locals\" id=\""+ local_id + "\" href=\"#\">" + localName + "</a>"); %></h4>
													<span class="member-role">
													<%out.println("Ogólna ocena uzytkownikow:"+ "  "); %>
													<br />
													<%out.println("Komentarzy: "+ commentsCount); %>
													</span>
													
													<% String opis1 = null;
													if (l.getDescription()== null || l.getDescription().equals("0")) opis1 = "Brak";
													else opis1 = l.getDescription();%>
													<p class="member-desc"><%out.println("Opis lokalu: "+ opis1);%></p>
													<br>
													<form role="form" id='addLocalRate'action="../addLocalRate" method="POST">
													Ocena: <select id="rate" name="rate" style="width: 10px;">
													<option value="0">0</option>
													<option value="1">1</option>
													<option value="2">2</option>
													<option value="3">3</option>
													<option value="4">4</option>
													<option value="5">5</option>
													</select>
														<input type="submit" class="mainBtn" value="Oceń" />	
													</form>
													<br>
													<div class="page-header">
	<h2 class="page-title" style="color: black;">Komentarze (<%=commentsCount %>)</h2>
</div>
<form role="form" id='localFeedback' action="../addLocalFeedback" method="POST">
		<p class="member-desc" style="color: white;">
		<input type="hidden" name="local_id" id="local_id" value="<%=local_id%>" />
		Jako: <input type="hidden" id="user" name="user" value="<%=user%>" />
		<% if (user != null){
		User u = um.id_to_user(sessionFactory, s, user);
		out.print(u.getUsername());
		 }else {
														out.print("Anonim");
														} %>
			<br>											
		<textarea name="comment" rows="3" class="form-control" id="comment"  placeholder="Tresc komentarza"></textarea>
				<input type="submit" class="mainBtn" value="Dodaj komentarz" />	
			</p>
		</form>
		<p class="member-desc" style="color:white;">
													<%
													if (listfb.size()> 0){
														for (LocalFeedback fb1 : listfb)
														{%>	<form role="form" id='deleteFb'action="../deleteLocalFeedback" method="POST">
														<input type="hidden" id="comment_id" name="comment_id" value="<%=fb1.getId()%>" />
<input type="submit" class="mainBtn" value="Usuń komentarz" />
</form>
															<%String userName = null;
															if(!fb1.getUser_id().equals("null")){
															String user_id = fb1.getUser_id();
															User u = um.id_to_user(sessionFactory, s, Integer.parseInt(user_id));
															 userName = u.getUsername();
															} else userName = "Anonim";
															String comment = fb1.getComment();
															String add_date = fb1.getAdd_date();
															%>
															<table>
															<tr>
															<td>
															Uzytkownik:
															</td>
															<td><%
															out.println(userName);
															%>
															</td>
															</tr>
															<tr>
															<td>
															Data dodania:
															</td>
															<td>
															<%out.println(add_date); %>
															</td></tr>
															<tr><td>
															Tresc komentarza:
															</td>
															<td><%
															out.println(comment);
															%>
															</td>
															</tr>
															</table><%
															out.println("<br />");
														}
														
													}
													else if (listfb.size() == 0) out.println("Ten lokal nie ma jeszcze zadnych komentarzy");
													%>
													</p>
												</div>
											</div>
										</div>
										
									
									</td>
									</tr>
									<tr> <td><br /></td></tr>
								
		
	
	</table>
	<%	s.getTransaction().commit();	%>

</div>		
		